﻿using ProgressTracker.Data.Abstract;
using ProgressTracker.Domain.Contracts;
using ProgressTracker.Domain.Messages;
using ProgressTracker.Domain.Translation;
using System;

namespace ProgressTracker.Domain.Services
{
    public class DayService : IDayService
    {
        private IDayMapper _dayMapper;

        private IDayRepository _dayRepo;
        private ITaskRepository _taskRepo;

        public DayService(
            IDayRepository dayRepo,
            ITaskRepository taskRepo,
            IDayMapper dayMapper)
        {
            _dayRepo = dayRepo;
            _dayMapper = dayMapper;
            _taskRepo = taskRepo;
        }

        public Day GetDay(DateTime date, string userEmail)
        {
            var day = _dayRepo.GetDay(date, userEmail);

            if(day == null)
            {
                day = _dayRepo.CreateNewDay(date, userEmail);
            }

            return _dayMapper.Create(day);
        }

        public void UpdateTaskCompleteness(UpdateTaskCompletenessRequest request)
        {
            var task = _taskRepo.GetTaskById(request.TaskId);
            var day = _dayRepo.GetDay(request.Date, request.UserEmail);

            day.UpdateTaskCompleteness(task, request.IsComplete);

            _dayRepo.Save(day);
        }

        public void UpdateDay(UpdateDayRequest request)
        {
            var day = _dayRepo.GetDay(request.Date, request.UserEmail);

            day.Notes = request.Notes;

            _dayRepo.Save(day);
        }
    }
}
